package com.huawei.appgallery.share.activity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.appgallery.share.protocol.SnsShareDialogActivityProtocol;
import com.huawei.appgallery.share.protocol.SnsShareDialogActivityProtocol.Request;
import com.huawei.appmarket.framework.activity.SecureActivity;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.api.Api.ApiOptions.HasOptions;
import com.huawei.hms.api.ConnectionResult;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.api.HuaweiApiClient.ConnectionCallbacks;
import com.huawei.hms.api.HuaweiApiClient.OnConnectionFailedListener;
import com.huawei.hms.support.api.client.ResultCallback;
import com.huawei.hms.support.api.entity.sns.SnsMsg;
import com.huawei.hms.support.api.hwid.HuaweiId;
import com.huawei.hms.support.api.hwid.HuaweiIdSignInOptions;
import com.huawei.hms.support.api.hwid.HuaweiIdSignInOptions.Builder;
import com.huawei.hms.support.api.hwid.SignInResult;
import com.huawei.hms.support.api.sns.HuaweiSns;
import com.huawei.hms.support.api.sns.IntentResult;
import java.lang.ref.WeakReference;
import o.cfw;
import o.cvt;
import o.cwj.i;
import o.cwr;
import o.cws;
import o.dgh;
import o.dgm;
import o.dgu;
import o.djl;
import o.dnx;
import o.ContentManager;
import o.evd;

public class SnsShareDialogActivity extends SecureActivity<SnsShareDialogActivityProtocol> {
    private byte[] ʻॱ;
    private String ʼॱ;
    private boolean ʾ = false;
    private cws ˈ = null;
    private byte[] ˊॱ;
    private String ˋॱ;
    private String ˏॱ;
    private HuaweiApiClient ͺ;
    private String ॱˊ;
    private boolean ॱˋ = false;
    private djl ॱˎ = null;
    private boolean ॱᐝ = false;
    private boolean ᐝॱ = false;

    static class a implements ConnectionCallbacks {
        private WeakReference<SnsShareDialogActivity> ˏ;

        public a(SnsShareDialogActivity snsShareDialogActivity) {
            this.ˏ = new WeakReference(snsShareDialogActivity);
        }

        public void onConnected() {
            cwr.ॱ.ˎ("SnsShareDialogActivity", "onConnected()");
            SnsShareDialogActivity snsShareDialogActivity = this.ˏ == null ? null : (SnsShareDialogActivity) this.ˏ.get();
            if (snsShareDialogActivity == null) {
                return;
            }
            if (snsShareDialogActivity.ॱˋ) {
                snsShareDialogActivity.finish();
            } else {
                snsShareDialogActivity.ॱॱ();
            }
        }

        public void onConnectionSuspended(int i) {
            cwr.ॱ.ˏ("SnsShareDialogActivity", "onConnectionSuspended, code: " + i);
            SnsShareDialogActivity snsShareDialogActivity = this.ˏ == null ? null : (SnsShareDialogActivity) this.ˏ.get();
            if (snsShareDialogActivity != null) {
                snsShareDialogActivity.finish();
            }
        }
    }

    static class c implements OnConnectionFailedListener {
        private WeakReference<SnsShareDialogActivity> ˎ;

        public c(SnsShareDialogActivity snsShareDialogActivity) {
            this.ˎ = new WeakReference(snsShareDialogActivity);
        }

        public void onConnectionFailed(ConnectionResult connectionResult) {
            if (this.ˎ == null) {
                Activity activity = null;
            } else {
                SnsShareDialogActivity snsShareDialogActivity = (SnsShareDialogActivity) this.ˎ.get();
            }
            if (activity == null) {
                return;
            }
            if (activity.ʾ) {
                activity.finish();
                return;
            }
            int errorCode = connectionResult.getErrorCode();
            cwr.ॱ.ˏ("SnsShareDialogActivity", "onConnectionFailed, code: " + errorCode);
            HuaweiApiAvailability instance = HuaweiApiAvailability.getInstance();
            if (instance.isUserResolvableError(errorCode)) {
                activity.ʾ = true;
                instance.resolveError(activity, errorCode, 1001);
                return;
            }
            activity.finish();
        }
    }

    static class d implements ResultCallback<SignInResult> {
        WeakReference<SnsShareDialogActivity> ˋ;

        public /* synthetic */ void onResult(Object obj) {
            ˊ((SignInResult) obj);
        }

        private d(SnsShareDialogActivity snsShareDialogActivity) {
            this.ˋ = new WeakReference(snsShareDialogActivity);
        }

        public void ˊ(SignInResult signInResult) {
            SnsShareDialogActivity snsShareDialogActivity = (SnsShareDialogActivity) this.ˋ.get();
            if (snsShareDialogActivity != null) {
                int statusCode = signInResult.getStatus().getStatusCode();
                if (statusCode == 0) {
                    snsShareDialogActivity.ˊ((Activity) snsShareDialogActivity);
                } else if (statusCode == 2001 && !snsShareDialogActivity.ॱˋ) {
                    try {
                        snsShareDialogActivity.startActivityForResult(signInResult.getData(), 1002);
                    } catch (ActivityNotFoundException e) {
                        cwr.ॱ.ˏ("SnsShareDialogActivity", "ActivityNotFoundException ：" + e.toString());
                    }
                } else if (statusCode == 2002) {
                    snsShareDialogActivity.ᐝ();
                    try {
                        snsShareDialogActivity.startActivityForResult(signInResult.getData(), 1003);
                    } catch (ActivityNotFoundException e2) {
                        cwr.ॱ.ˏ("SnsShareDialogActivity", "ActivityNotFoundException ：" + e2.toString());
                    }
                } else {
                    snsShareDialogActivity.finish();
                }
            }
        }
    }

    static class e implements ResultCallback<IntentResult> {
        private WeakReference<Activity> ˏ;

        public /* synthetic */ void onResult(Object obj) {
            ॱ((IntentResult) obj);
        }

        public e(Activity activity) {
            this.ˏ = new WeakReference(activity);
        }

        public void ॱ(IntentResult intentResult) {
            Activity activity = (Activity) this.ˏ.get();
            if (activity == null) {
                cwr.ॱ.ˎ("SnsShareDialogActivity", "ResultCallbackImpl activity null");
                return;
            }
            int statusCode = intentResult.getStatus().getStatusCode();
            if (statusCode == 0) {
                try {
                    activity.startActivityForResult(intentResult.getIntent(), 1004);
                    return;
                } catch (ActivityNotFoundException e) {
                    cwr.ॱ.ˏ("SnsShareDialogActivity", "ActivityNotFoundException ：" + e.toString());
                    return;
                }
            }
            Toast.makeText(activity, i.ॱᐝ, 0).show();
            cwr.ॱ.ˏ("SnsShareDialogActivity", "sns share sendMsg error,code: " + statusCode);
            activity.finish();
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().requestFeature(1);
        evd.ˏ(getWindow());
        ʼ();
        SnsShareDialogActivityProtocol snsShareDialogActivityProtocol = (SnsShareDialogActivityProtocol) ʾ();
        if (snsShareDialogActivityProtocol == null) {
            finish();
            return;
        }
        Request ॱ = snsShareDialogActivityProtocol.ॱ();
        if (ॱ == null) {
            finish();
            return;
        }
        this.ˋॱ = ॱ.ˊ();
        this.ॱˊ = ॱ.ˎ();
        this.ˏॱ = ॱ.ˋ();
        this.ʼॱ = ॱ.ʼ();
        this.ˊॱ = ॱ.ॱ();
        this.ʻॱ = ॱ.ˏ();
        this.ͺ.connect(null);
        this.ᐝॱ = ॱ.ᐝ();
    }

    public void onDestroy() {
        if (this.ͺ != null) {
            this.ͺ.disconnect();
        }
        super.onDestroy();
    }

    public void onResume() {
        super.onResume();
        if (this.ॱᐝ && !ˊॱ()) {
            finish();
        }
    }

    private void ʼ() {
        HasOptions build = new Builder(HuaweiIdSignInOptions.DEFAULT_SIGN_IN).build();
        ConnectionCallbacks aVar = new a(this);
        this.ͺ = new HuaweiApiClient.Builder(this).addApi(HuaweiId.SIGN_IN_API, build).addApi(HuaweiSns.API).addScope(HuaweiId.HUAEWEIID_BASE_SCOPE).addConnectionCallbacks(aVar).addOnConnectionFailedListener(new c(this)).build();
    }

    private void ॱॱ() {
        if (this.ͺ.isConnected()) {
            HuaweiId.HuaweiIdApi.signInBackend(this.ͺ).setResultCallback(new d());
        } else {
            cwr.ॱ.ˏ("SnsShareDialogActivity", "sign in client is not connected");
        }
    }

    private void ˊ(Activity activity) {
        String string = getResources().getString(i.ˋ);
        SnsMsg snsMsg = new SnsMsg();
        if (this.ᐝॱ) {
            snsMsg.setTitle(this.ॱˊ);
            snsMsg.setDescription("");
        } else {
            snsMsg.setTitle(this.ˋॱ);
            snsMsg.setDescription(this.ॱˊ);
        }
        snsMsg.setUrl(this.ˏॱ);
        PackageInfo ˏ = dnx.ˏ("com.huawei.hwid", getBaseContext());
        if (!this.ᐝॱ || ˏ == null || ˏ.versionCode < HuaweiApiAvailability.HMS_VERSION_CODE_OD) {
            snsMsg.setAppName(string);
        } else {
            snsMsg.setAppName(this.ˋॱ);
        }
        if (!TextUtils.isEmpty(getPackageName())) {
            snsMsg.setTargetAppPackageName(getPackageName());
            int i = 0;
            try {
                i = Integer.parseInt(getString(i.ॱˊ));
            } catch (NumberFormatException e) {
                cwr.ॱ.ˏ("SnsShareDialogActivity", "sns share sendMsg error, can not get targerVersion: ");
            }
            snsMsg.setTargetAppVersionCode(i);
        }
        snsMsg.setTargetAppMarketId(getString(i.ͺ));
        snsMsg.setCheckTargetApp(true);
        if (this.ˊॱ != null) {
            snsMsg.setLinkIconData(this.ˊॱ);
        }
        if (this.ʻॱ != null && this.ᐝॱ) {
            snsMsg.setAppIconData(this.ʻॱ);
        }
        if (HuaweiSns.HuaweiSnsApi != null) {
            HuaweiSns.HuaweiSnsApi.getMsgSendIntent(this.ͺ, snsMsg, 1, true).setResultCallback(new e(activity));
        }
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        cwr.ॱ.ˋ("SnsShareDialogActivity", "onActivityResult, resultCode: " + i2);
        switch (i) {
            case 1001:
                ˊ(i2, intent);
                return;
            case 1002:
                this.ॱˋ = true;
                ॱॱ();
                return;
            case 1003:
                ॱˊ();
                SignInResult hwIdSignInResultFromIntent = HuaweiId.HuaweiIdApi.getHwIdSignInResultFromIntent(intent);
                if (hwIdSignInResultFromIntent.isSuccess()) {
                    ˊ((Activity) this);
                    return;
                }
                Toast.makeText(this, i.ॱᐝ, 0).show();
                cwr.ॱ.ˏ("SnsShareDialogActivity", "onResult, SignInResult-Status: " + hwIdSignInResultFromIntent.getStatus().toString());
                finish();
                return;
            case 1004:
                if (i2 == -1) {
                    String userId = UserSession.getInstance().getUserId();
                    dgh.onEvent(new o.dge.d(ContentManager.getInstant().getContext(), i.ˎ).ˏ("05|00|" + userId + '|' + this.ˏॱ).ˋ());
                    cvt ˋ = dgu.ˋ("8", this.ʼॱ, dgm.ˎ((Activity) this));
                    ˋ.ˊ("HWIM");
                    this.ˈ = new cws(this);
                    cfw.ˎ(ˋ, this.ˈ);
                    return;
                }
                finish();
                return;
            default:
                finish();
                return;
        }
    }

    private void ˊ(int i, Intent intent) {
        if (i != -1 || intent == null) {
            cwr.ॱ.ˋ("SnsShareDialogActivity", "resultCode is not OK");
        } else {
            this.ʾ = false;
            if (intent.getIntExtra(BridgeActivity.EXTRA_RESULT, -1) == 0) {
                if (!this.ͺ.isConnecting() && !this.ͺ.isConnected()) {
                    this.ͺ.connect(null);
                    return;
                }
                return;
            }
        }
        this.ॱᐝ = true;
    }

    private void ᐝ() {
        if (this.ॱˎ == null) {
            this.ॱˎ = new djl(this);
            this.ॱˎ.ˋ(getString(i.ʾ));
            this.ॱˎ.show();
            return;
        }
        this.ॱˎ.show();
    }

    private void ॱˊ() {
        if (this.ॱˎ != null) {
            this.ॱˎ.dismiss();
        }
    }

    public void finish() {
        if (this.ˈ != null) {
            this.ˈ.ˏ();
        }
        super.finish();
    }

    private boolean ˊॱ() {
        return (this.ˈ == null || this.ˈ.ˊ() == null) ? false : true;
    }
}
